package com.api.customization.huangwm.formmode.dbField;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.apache.axis.components.logger.LogFactory;
import org.apache.commons.logging.Log;

import weaver.conn.RecordSet;

import com.customization.huangwm.utils.DateTools;
import com.engine.common.util.ParamUtil;

/**
 * @author Huangwm
 * @package com.api.customization.huangwm.formmode.dbField
 * @description
 */
public class TableFieldsOperation {
    private Log log = LogFactory.getLog("custom_huangwm");

    @POST
    @Path("/getTableFields")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getTableFields(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiResult = new HashMap<String, Object>();
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        log.info("TableFieldsOperation--->进入getTableFields方法 " + new DateTools().getCurrentTimes());
        try {
            Map<String, Object> requestParams = ParamUtil.request2Map(request);
            String billid = requestParams.get("billid").toString();
            String sql1 = "select id,tablename,namelabel from workflow_bill bill where id=" + billid;
            RecordSet rs1 = new RecordSet();
            rs1.execute(sql1);
            if (rs1.next()) {
                apiResult.put("billid", billid);
                apiResult.put("tablename", rs1.getStringNoTrim("tablename"));
                String sql2 = "select id,fieldname,labelname,(case  when  detailtable<>'' then detailtable else '" + rs1.getStringNoTrim("tablename") + "' end)  as tablename" +
                        "\n from workflow_billfield left join  HtmlLabelInfo label on workflow_billfield.fieldlabel=label.indexid " +
                        "\n where languageid=7 and billid=" + billid;
                RecordSet rs2 = new RecordSet();
                rs2.execute(sql2);
                while (rs2.next()) {
                    Map<String, String> data = new HashMap<>();
                    data.put("fieldid", rs2.getStringNoTrim("id"));
                    data.put("fieldname", rs2.getStringNoTrim("fieldname"));
                    data.put("labelname", rs2.getStringNoTrim("labelname"));
                    data.put("tablename", rs2.getStringNoTrim("tablename"));
                    list.add(data);
                }
            }
        } catch (Exception e) {
            apiResult.put("apiStatus", false);
            apiResult.put("msg", e.getMessage());
            return apiResult;
        }
        apiResult.put("apiStatus", true);
        apiResult.put("list", list);
        log.info(list);
        return apiResult;
    }
}
